import { LinRouter, NotFound, disableLoading } from 'lin-mizar';
import { groupRequired } from '../../middleware/jwt';
import {
  BookSearchValidator,
  CreateOrUpdateBookValidator
} from '../../validator/book';
import { PositiveIdValidator } from '../../validator/common';

import { getSafeParamId } from '../../lib/util';
import { BookNotFound } from '../../lib/exception';
import { CourseDao } from '../../dao/course';
import { emailTo } from '../../lib/email';
import { arr } from '../../config/email-list';

// course 的红图实例
const courseApi = new LinRouter({
  prefix: '/v1/course',
  module: '图书'
});

// course 的dao 数据库访问层实例
const courseDto = new CourseDao();

courseApi.get('/:id', async ctx => {
  const v = await new PositiveIdValidator().validate(ctx);
  const id = v.get('path.id');
  const course = await courseDto.getCourse(id);
  if (!course) {
    throw new NotFound({
      code: 10022
    });
  }
  ctx.json(course);
});

courseApi.get('/', async ctx => {
  const courses = await courseDto.getCourses();
  if (!courses || courses.length < 1) {
    throw new NotFound({
      message: '没有找到相关书籍'
    });
  }
  ctx.json(courses);
});

courseApi.get('/search/one', async ctx => {
  const v = await new BookSearchValidator().validate(ctx);
  const course = await courseDto.getCourseByKeyword(v.get('query.q'));
  if (!course) {
    throw new BookNotFound();
  }
  ctx.json(course);
});

courseApi.post('/', async ctx => {
  const v = await new CreateOrUpdateBookValidator().validate(ctx);
  await courseDto.createCourse(v);
  ctx.success({
    code: 12
  });
});

courseApi.get('/send/one', async ctx => {

  let html = `<h1>市面收费IT课程免费领取，来就送。需要的可以看一下</h1>
  选课地址：<a href="https://www.yuque.com/chenxiaoqiang-brnye/itbao">点这里</a><br />
  <span>极客时间专栏8元一门，打包68元，慕课实战低至9.9</span>
  <a href="https://javacm.com">网站选课点这里,首次免费领课</a><br />
  大致内容目录：
  <ul>
    <li><a href="https://www.yuque.com/chenxiaoqiang-brnye/itbao">慕课网点这里</a></li>
    <li><a href="https://www.yuque.com/chenxiaoqiang-brnye/itbao">极客时间点这里</a></li>
    <li><a href="https://www.yuque.com/chenxiaoqiang-brnye/itbao">前端点这里</a></li>
    <li><a href="https://www.yuque.com/chenxiaoqiang-brnye/itbao">GO点这里</a></li>
  </ul>
  课程类型

  <div>客服微信：cbq2393159</div>
  <div style='color:red;font-weight:800;'>首次添加，都会赠送实战课程。感谢您的支持，如有打扰，十分抱歉。</div>
  `

  //   let html = `
  //   小七找课家主营稀缺IT资源，稀缺金融课程，另有设计课程，考研考证，网赚等课程……您想要的课程我都有。
  // 低价学习，助您升职加薪。
  // <div>客服微信：Leo20011115</div>
  // 选课地址：<a href="https://www.yuque.com/books/share/e59e6bcb-13b0-40c0-bd1c-ecab6d0217f1">点这里</a><br />
  //   `


  // let qq = arr.map((item) => {
  //   return item + '@qq.com'
  // }).join(',')

  for (var i = 0; i < arr.length; i += 30) {
    let result = []
    result.push(arr.slice(i, i + 30))
    let qq = result.map((item) => {
      return item + '@qq.com'
    }).join(',')
    await emailTo(qq, '打扰一下，没有恶意，推荐一个资源网站javacm.com，首次访问可以获取网上付费学习资料。', '', html, function (b) {
      console.log(b, 'info')
    })
  }

  console.log('end')
  // emailTo(qq, '您好，同学我这边有很多网课IT资源，首次加我免费赠送课，自己挑选哦。', '', html, function (a) {
  //   console.log(a, '完成')
  // })
  ctx.success({
    code: 12
  });
});
courseApi.put('/:id', async ctx => {
  const v = await new CreateOrUpdateBookValidator().validate(ctx);
  const id = getSafeParamId(ctx);
  await courseDto.updateCourse(v, id);
  ctx.success({
    code: 13
  });
});

courseApi.linDelete(
  'deleteBook',
  '/:id',
  courseApi.permission('删除资源'),
  groupRequired,
  async ctx => {
    const v = await new PositiveIdValidator().validate(ctx);
    const id = v.get('path.id');
    await courseDto.deleteCourse(id);
    ctx.success({
      code: 14
    });
  }
);

module.exports = { courseApi, [disableLoading]: false };
